// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
    traceUser: true,
    env: 'aaa111-9gh9fp5ieac4de5d'
})

const db = cloud.database()
exports.main = async (event, context) => {
    const wxContext = cloud.getWXContext()
    var isChecked = await db.collection('userChecked').where({
        checklistid: event.checkid,
        _openid: wxContext.OPENID,
        tripid: event.tripid
    }).count()
    if (isChecked.total > 0) {
        //删除
        await db.collection('userChecked').where({
            checklistid: event.checkid,
            _openid: wxContext.OPENID,
            tripid: event.tripid
        }).remove()
    } else {
        //add
        await db.collection('userChecked').add({
            data: {
                checklistid: event.checkid,
                _openid: wxContext.OPENID,
                tripid: event.tripid
            }
        })
    }
    return true
}